#!/usr/bin/perl

# acceptable forms:
#   ctattach                     - give usage message
#   ctattach project             - attach to the personal flavor of the project
#   ctattach project flavor      - attach to a specific flavor of the project
#   ctattach -                   - list projects that can be attached to
#   ctattach project -           - list flavors of a given project
#   ctattach - flavor            - list projects with a certain flavor
#   ctattach -def project flavor - attach to project, setting CTDEFAULT_FLAV
#                                  to flavor for the scope of this attach

sub CTAttachUsage {
   print STDERR "Usage: ctattach -def project flavor  -or-\n" ;
   print STDERR "       ctattach project [flavor]     -or-\n" ;
   print STDERR "       ctattach project -            -or-\n" ;
   print STDERR "       ctattach - [flavor]\n" ;
   &CTAttachWriteNullScript( $tmpname ) ;
   print $tmpname . "\n" ;
   exit;
}

$tool = $ENV{"DTOOL"} ;
if ( $tool eq "" ) {
    die "\$" . "DTOOL environment must be set to use CTtools\n" ;
}

require "$tool/built/include/ctattch.pl" ;

$tmpname = "/tmp/script.$$" ;

if ( $#ARGV == -1 ) {
    &CTUDebug( "got no arguments\n" ) ;
    &CTAttachUsage ;
}

$idx = 0 ;
$proj = "" ;
$flav = "" ;
$noflav = 0 ;
$defflav = "" ;
$spread = 0 ;
$anydef = 0 ;

#
# parse arguemnts
#

if ( $ARGV[$idx] eq "-def" ) {
   &CTUDebug( "got '-def' parameter\n" ) ;
   if ( $#ARGV < ($idx + 2) ) {
       &CTUDebug( "not enough arguments after -def\n" ) ;
       &CTAttachUsage ;
   }
   $defflav = $ARGV[$idx+2] ;
   $spread = 1;
   &CTUDebug( "spread default flavor is '$defflav'\n" ) ;
   $idx++ ;
} else {
   if ( $ENV{"CTDEFAULT_FLAV"} ne "" ) {
      $defflav = $ENV{"CTDEFAULT_FLAV"} ;
      &CTUDebug( "environment default flavor is '$defflav'\n" ) ;
   }
}

$proj = $ARGV[$idx] ;
&CTUDebug( "project is '$proj'\n" ) ;

if ( $defflav eq "" ) {
   $defflav = "default" ;
   &CTUDebug( "no environmental default, using 'default'\n" ) ;
}

if ( $#ARGV > $idx ) {
   $flav = $ARGV[$idx+1] ;
   &CTUDebug( "provided flavor is '$flav'\n" ) ;
} else {
   if ( $proj ne "-" ) {
      $flav = $defflav;
      &CTUDebug( "using environment default flavor '$flav'\n" ) ;
      $noflav = 1 ;
   }
}

if (( $noflav == 1 ) || ( $flav eq "default" )) {
   $anydef = 1 ;
}

#
# act on the arguments we got
#

require "$tool/built/include/ctquery.pl" ;
require "$tool/built/include/ctvspec.pl" ;

if (( $proj eq "-" ) || ( $flav eq "-" )) {
   if ( $#ARGV == 0 ) {
      # list projects that can be attached to
      print STDERR "Projects that can be attached to:\n" ;
      $_ = &CTListAllProjects ;
      @projlist = split ;
      foreach $item ( @projlist ) {
	 print STDERR "   $item\n" ;
      }
   } elsif ( $proj eq "-" ) {
      # list project that have a given flavor
      print STDERR "Projects that have a '$flav' flavor:\n" ;
      $_ = &CTListAllProjects ;
      @projlist = split ;
      foreach $item ( @projlist ) {
	 $tmp = &CTResolveSpec( $item, $flav ) ;
	 if ( $tmp ne "" ) {
	    print STDERR "   $item\n" ;
	 }
      }
   } else {
      # list flavors of a given project
      print STDERR "Flavors of project '$proj':\n" ;
      $_ = &CTListAllFlavors( $proj ) ;
      @flavlist = split ;
      foreach $item ( @flavlist ) {
	 print STDERR "   $item\n" ;
      }
   }
   &CTAttachWriteNullScript( $tmpname ) ;
   print $tmpname . "\n" ;
} else {
   # output a real attachment
   $curflav = &CTQueryProj( $proj ) ;
   if (( $curflav eq "" ) || ( $noflav == 0 )) {
      $envsep{"PATH"} = ":" ;
      $envsep{"LD_LIBRARY_PATH"} = ":" ;
      $envsep{"DYLD_LIBRARY_PATH"} = ":" ;
      $envsep{"PFPATH"} = ":" ;
      $envsep{"SSPATH"} = ":" ;
      $envsep{"STKPATH"} = ":" ;
      $envsep{"DC_PATH"} = ":" ;
      $spec = &CTAttachCompute( $proj, $flav, $anydef ) ;
      if ( $spec eq "" ) {
	 &CTAttachWriteNullScript( $tmpname ) ;
	 print $tmpname . "\n" ;
      } else {
	 &CTAttachWriteScript( $tmpname ) ;
	 print $tmpname . "\n" ;
      }
   } else {
      &CTAttachWriteNullScript( $tmpname ) ;
      print $tmpname . "\n" ;
   }
}
